﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption
Imports AugmentLib.WebComponent

Partial Class AnnouncementView
    Inherits System.Web.UI.Page

  

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        Else
            lblhidStaffId.Text = Session("StaffId").ToString
        End If

        If Not Page.IsPostBack Then

            If Not Session("id") Is Nothing Then
                lblhidAnnouncementId.Text = Me.Session("id").ToString
                LoadAnnouncement(lblhidAnnouncementId.Text)
            End If

        End If
    End Sub

    Private Sub LoadAnnouncement(ByVal AnnouncementId As String)
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT Title, Priority, Content, Attachment, AttDesc FROM AW_Announcement WHERE AnnouncementId=@AnnouncementID"
        objDB.AddParameter("AnnouncementID", Data.SqlDbType.UniqueIdentifier, New Guid(AnnouncementId))
        objDB.FillData(TempDT, "AW_Announcement", Database.SQLCommandType.NormalString)

        For Each ReadRow As Data.DataRow In TempDT.Rows
            lblTitle.Text = ReadRow.Item("Title").ToString
            Select Case ReadRow.Item("Priority").ToString
                Case "1"
                    'lblPriority.Text = "Priority - HIGH"
                    StatusHigh.Visible = True

                Case "2"
                    'lblPriority.Text = "Priority - MEDIUM"
                    StatusMedium.Visible = True

                Case "3"
                    'lblPriority.Text = "Priority - LOW"
                    StatusLow.Visible = True

            End Select
            'Replace(lbl.Text, Chr(13) + Chr(10), "<br/>")
            lblContent.Text = Replace(ReadRow.Item("Content").ToString, Chr(13) + Chr(10), "<br/>")

            'lblContent.Text = "The quick brown fox jumps over the lazzy dog. The quick brown fox jumps over the lazzy dog. The quick brown fox jumps over the lazzy dog. The quick brown fox jumps over the lazzy dog. The quick brown fox jumps over the lazzy dog. The quick brown fox jumps over the lazzy dog."
            If ReadRow.Item("Attachment").ToString = "" Then
                lnkAttach.Visible = False
            Else
                lnkAttach.Visible = True
                lblAttachment.Text = ReadRow.Item("Attachment").ToString
                If ReadRow.Item("AttDesc").ToString = "" Then
                    lnkAttach.Text = "Attachment"
                Else
                    lnkAttach.Text = ReadRow.Item("AttDesc").ToString
                End If
            End If
        Next
    End Sub


    Protected Sub lnkAttach_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAttach.Click
        Dim path As String = Server.MapPath("attachment/" & lblAttachment.Text) 'get file object as FileInfo
        Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) '-- if the file exists on the server
        If file.Exists Then 'set appropriate headers
            Response.Clear()
            Response.AddHeader("Content-Disposition", "attachment; filename=" & lnkAttach.Text & file.Extension)
            Response.AddHeader("Content-Length", file.Length.ToString())
            Response.ContentType = "application/octet-stream"
            Response.WriteFile(file.FullName)
            Response.End() 'if file does not exist
        Else
            lblMsgError.Text = "This file does not exist!"
            divError.Visible = True
        End If 'nothing in the URL as HTTP GET
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("Dashboard.aspx")
    End Sub
End Class
